package com.hna.iot.common.utils;
/*
package com.hnair.bot.common.utils;

import com.hnair.bot.model.BusStudentpojo;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

*/
/**
 * Created by Chen-D.W on 2017/8/2.
 * 读取excel内容并进行封装
 *//*

public class ReadExcel {
    //导入Excel文件
    protected File excel;

    //封装要上传的文件
    public File getExcel() {
        return excel;
    }

    public void setExcel(File excel) {
        this.excel = excel;
    }

    */
/**
     * @Author Chen-DongWei  com.hnair.bot
     * Date 2017/8/3 13:19
     * @Descrintion 读取excel
     *//*

    public List<BusStudentpojo> importExcel() {
        List<BusStudentpojo> success = new ArrayList();
        if (excel != null) {
            try {
                HSSFWorkbook wb=null;
                try{
                    //读取excel文件分析Excel文件中的数据
                     wb = new HSSFWorkbook(new FileInputStream(excel));
                }catch (Exception e){
                    throw new Exception("请选择Excel文件");
                }

                //读取第一页的内容
                HSSFSheet sheet = wb.getSheetAt(0);
                for (int i = 1; i <=sheet.getLastRowNum(); i++) {
                    HSSFRow row = sheet.getRow(i);
                    if (row != null) {
                        HSSFCell stuname = row.getCell(0);
                        HSSFCell sex = row.getCell(1);
                        HSSFCell birthday = row.getCell(2);
                        HSSFCell busflag = row.getCell(3);
                        HSSFCell parentname = row.getCell(4);
                        HSSFCell parentno = row.getCell(5);
                        HSSFCell relation = row.getCell(6);
                        HSSFCell mobile1 = row.getCell(7);
                        HSSFCell mobile2 = row.getCell(8);
                        HSSFCell grouplevel = row.getCell(9);
                        HSSFCell groupnum = row.getCell(10);
                        BusStudentpojo student = new BusStudentpojo();
                        student.setStuName(ExcelUtils.getStringCellValues(stuname));
                        student.setSex(ExcelUtils.getStringCellValues(sex));
                        Format format = new SimpleDateFormat("yyyy-MM-dd");
                        Date date = (Date) format.parseObject(ExcelUtils.getDateCellValue(birthday));
                        student.setBirthday(date);
                        student.setBusFlag(ExcelUtils.getStringCellValues(busflag));
                        student.setParentName(ExcelUtils.getStringCellValues(parentname));
                        student.setParentNo(ExcelUtils.getStringCellValues(parentno));
                        student.setRelation(ExcelUtils.getStringCellValues(relation));
                        student.setMobile1(ExcelUtils.getStringCellValues(mobile1));
                        student.setMobile2(ExcelUtils.getStringCellValues(mobile2));
                        student.setGroupLevel(ExcelUtils.getStringCellValues(grouplevel));
                        student.setGroupNum(ExcelUtils.getStringCellValues(groupnum));
                        //检查用户输入是否合法
                        if (student.getStuName().equals("")) {
                            throw new Exception("姓名格式不正确，请检查第" + (i) + "行第" + 1 + "列!");
                        } else if (student.getSex().equals("")) {
                            throw new Exception("性别格式不正确，请检查第" + (i) + "行第" + 2 + "列!");
                        } else if (student.getBirthday().equals("")) {
                            throw new Exception("出生日期格式不正确，请检查第" + (i) + "行第" + 3 + "列!");
                        } else if (student.getBusFlag().equals("")) {
                            throw new Exception("是否乘坐班车格式不正确，请检查第" + (i) + "行第" + 4 + "列!");
                        } else if (student.getParentName().equals("")) {
                            throw new Exception("家长姓名格式不正确，请检查第" + (i) + "行第" + 5 + "列!");
                        } else if (student.getParentNo().equals("")) {
                            throw new Exception("身份证格式不正确，请检查第" + (i) + "行第" + 6 + "列!");
                        } else if (student.getRelation().equals("")) {
                            throw new Exception("身份关系格式不正确，请检查第" + (i) + "行第" + 7 + "列!");
                        } else if (student.getMobile1().equals("")) {
                            throw new Exception("手机号1格式不正确，请检查第" + (i) + "行第" + 8 + "列!");
                        } else if (student.getMobile2().equals("")) {
                            throw new Exception("手机号2格式不正确，请检查第" + (i) + "行第" + 9 + "列!");
                        }else if (student.getGroupLevel().equals("")) {
                            throw new Exception("年级格式不正确，请检查第" + (i) + "行第" + 10+ "列!");
                        } else if (student.getMobile2().equals("")) {
                            throw new Exception("班号格式不正确，请检查第" + (i) + "行第" + 11 + "列!");
                        }
                        success.add(student);
                    }

                }
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
        }
        return success;
    }

    */
/**
     * @Author Chen-DongWei  com.hnair.bot
     * Date 2017/8/2 20:59
     * @Descrintion 上传到服务器D://目录下
     *//*

    public String upload(MultipartFile file) {
        try {
            if (file == null) {
                throw new Exception("上传失败：文件为空");
            }
            if (file.getSize() > 10000000) {
                throw new Exception("上传失败：文件大小不能超过10M");
            }
            //得到文件名
            String filename = file.getOriginalFilename();

            if (file.getSize() > 0) {
                try {
                    SaveFileFromInputStream(file.getInputStream(), "D:/", filename);
                } catch (IOException e) {
                    throw new Exception("上传失败：上传文件不能为空");
                }
            } else {
                throw new Exception("上传失败：上传文件不能为空");
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex.getMessage());
        }
        return null;
    }

    */
/**
     * @Author Chen-DongWei  com.hnair.bot
     * Date 2017/8/2 21:00
     * @Descrintion 以字节流传输
     *//*

    public void SaveFileFromInputStream(InputStream stream, String path, String filename) throws IOException {
        FileOutputStream fs = new FileOutputStream(path + "/" + filename);
        byte[] buffer = new byte[1024 * 1024];
        int bytesum = 0;
        int byteread = 0;
        while ((byteread = stream.read(buffer)) != -1) {
            bytesum += byteread;
            fs.write(buffer, 0, byteread);
            fs.flush();
        }
        fs.close();
        stream.close();
    }
}
*/
